package com.yf.zzmusic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yf.zzmusic.common.PageInfo;
import com.yf.zzmusic.pojo.dto.comments.CommentsPageDTO;
import com.yf.zzmusic.pojo.entity.Comments;
import com.yf.zzmusic.mapper.CommentsMapper;
import com.yf.zzmusic.pojo.entity.MusicCollect;
import com.yf.zzmusic.pojo.vo.comments.CommentListVO;
import com.yf.zzmusic.service.ICommentsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 评论表	主要逻辑	user_id 和 type 和 target_id 不可同时一样		 服务实现类
 * </p>
 *
 * @author
 * @since 2024-03-07
 */
@Service
public class CommentsServiceImpl extends ServiceImpl<CommentsMapper, Comments> implements ICommentsService {

  @Autowired
  CommentsMapper commentsMapper;
  @Override
  public PageInfo commentsByPage(CommentsPageDTO commentsPageDTO) {
    QueryWrapper<Comments> queryWrapper = new QueryWrapper<>();
    Integer pageNum = commentsPageDTO.getPageNum();
    Integer pageSize = commentsPageDTO.getPageSize();
    Integer userId=commentsPageDTO.getUserId();
    Integer type=commentsPageDTO.getType();
    Integer targetId= commentsPageDTO.getTargetId();
    //判断pageNum与pageSize
    if (pageNum == null) {
      pageNum = 0;
    }
    if (pageSize == null) {
      pageSize = 1000;
    }
    if (userId != null) {
      queryWrapper.eq("user_id", userId);
    }
    if (type != null) {
      queryWrapper.eq("type", type);
    }
    if (targetId != null) {
      queryWrapper.eq("target_id", targetId);
    }
    Page<Comments> commentsPage = page(new Page<>(pageNum, pageSize), queryWrapper);
    PageInfo<Comments> commentsPageInfo = new PageInfo<>();
    commentsPageInfo.setPageNum(pageNum);
    commentsPageInfo.setPageSize(pageSize);
    commentsPageInfo.setTotal(commentsPage.getTotal());
    commentsPageInfo.setData(commentsPage.getRecords());
    return commentsPageInfo;
  }

  @Override
  public PageInfo<CommentListVO> commentListByPage(CommentsPageDTO commentsPageDTO) {
    Integer pageNum=commentsPageDTO.getPageNum();
    Integer pageSize=commentsPageDTO.getPageSize();
    Integer total=commentsMapper.commentListCount(commentsPageDTO);
    //判断pageNum与pageSize
    if (pageNum == null) {
      pageNum = 0;
    }
    if (pageSize == null) {
      pageSize = 1000;
    }
    //执行SQL
    //获取评论列表
    List<CommentListVO> commentListVOS=commentsMapper.commentsList(commentsPageDTO);
    //封装pageInfo
    Page<Comments> commentsPage = page(new Page<>(pageNum, pageSize));
    PageInfo<CommentListVO> commentsPageInfo = new PageInfo<>();
    commentsPageInfo.setPageNum(pageNum);
    commentsPageInfo.setPageSize(pageSize);
    /** 这里再次调用mapper，获取准确的数据总条数 */
    commentsPageInfo.setTotal(total);
    commentsPageInfo.setData(commentListVOS);
    return commentsPageInfo;
  }

}
